/**
 * Sybil Project Management
 * 
 * Sybil is an client/server-based application for managing projects and
 * resources.
 * 
 * Copyright (C) 2009 Natascha Chrobok <nat@sybil-pm.org>
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.sybil_pm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.*;

/**
 * Servlet implementation class RequestHub
 */
public class RequestHub extends HttpServlet {
	private static final long serialVersionUID = 1L;

	static Logger log=Logger.getLogger(RequestHub.class);

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RequestHub() {
        super();
        BasicConfigurator.configure();
        // TODO Auto-generated constructor stub
    }

    protected void doHead(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, java.io.IOException {
    	log.info("doHead");
		setResponseHeader(response);
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, 
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
    	log.info("doGet");
		setResponseHeader(response);
		PrintWriter wr=response.getWriter();
		wr.print("<html>sybil project management debug page (GET)<p/>");
		wr.print("<form method=\"POST\" action=\"http://127.0.0.1:8080/SybilServer/RequestHub\">");
		wr.print("<p>Type something: <input name=\"something\">");
		wr.print("<p>Press <input type=\"submit\" value=\"here\"> to submit");
		wr.print("</form></html>");
		wr.flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, 
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
    	log.info("doPost");
    	if(log.isEnabledFor(Level.DEBUG))
    	{
    		BufferedReader rd=request.getReader();
    		String text=rd.readLine();
    		while(text!=null)
    		{
    			log.debug(text);
    			text=rd.readLine();
			}
		}
		setResponseHeader(response);
		PrintWriter wr=response.getWriter();
		wr.print("<html>sybil project management debug page (POST)</html>");
		wr.flush();
	}

	private void setResponseHeader(HttpServletResponse response) {
		response.setHeader("Pragma", "sybil-app 0 0 1");
	}
}
